package edu.mis.modules.customer;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import edu.mis.model.Customer;

public class CustomerController extends Controller {
	private static CustomerService service = new CustomerService();
	public void index() {
		render("customer.html");
	}
	public void list() {
		setAttr("data",service.findAllCustomer());
		renderJson();
	}
	
	@Before(CustomerValidator.class)
	public void save() {
		Customer customer = getModel(Customer.class);
		if (service.saveCustomer(customer)) {
			setAttr("result", true);
			setAttr("msg", "添加客户成功");
		} else {
			setAttr("result", false);
		}
		renderJson();
	}
	
	@Before(CustomerValidator.class)
	public void update() {
		Customer customer = getModel(Customer.class);
		if (service.updateCustomer(customer)) {
			setAttr("result", true);
			setAttr("msg", "客户信息修改成功!");
		} else {
			setAttr("result", false);
		}
		renderJson();
	}
	
	public void delete() {
		if (service.deleteCustomerById(getParaToInt("id"))) {
			setAttr("result", true);
			setAttr("msg", "删除客户成功");
		} else {
			setAttr("result", false);
		}
		renderJson();
	}
}